<template>
  <div class="goodsadd">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品添加</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="mt16">
      <!-- 提示信息 -->
      <el-alert
        title="添加商品信息"
        show-icon
        type="info"
        :active="active"
        center
        :closable="false"
      >
      </el-alert>
      <!-- 表单 -->
      <el-form :model="addGoodsData">
        <!-- 步骤条 -->
        <el-steps :active="activeName * 1" finish-status="success" align-center>
          <el-step title="基本信息"></el-step>
          <el-step title="商品参数"></el-step>
          <el-step title="商品属性"></el-step>
          <el-step title="商品图片"></el-step>
          <el-step title="商品内容"></el-step>
          <el-step title="完成"></el-step>
        </el-steps>
        <!-- tabs栏 -->
        <el-tabs
          v-model="activeName"
          class="mt16"
          tab-position="left"
          @tab-click="handleClick"
          :before-leave="beforeLeave"
        >
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称">
              <el-input
                v-model="addGoodsData.goods_name"
                placeholder="请输入商品名称"
                class="mt16"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
              <el-input
                type="number"
                v-model="addGoodsData.goods_price"
                placeholder="请输入商品价格"
                class="mt16"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品重量">
              <el-input
                type="number"
                v-model="addGoodsData.goods_weight"
                placeholder="请输入商品重量"
                class="mt16"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量">
              <el-input
                type="number"
                v-model="addGoodsData.goods_number"
                placeholder="请输入商品数量"
                class="mt16"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <p>
                当前选中的分类数组: {{ checkArr }}--当前商品分类:{{
                  current_cate_id
                }}
              </p>
              <el-cascader
                v-model="checkArr"
                :options="options"
                placeholder="请选择"
                class="mt16"
                :props="defaultProps"
                @tab-click="handleChange"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <!-- 动态参数 -->
          <el-tab-pane label="商品参数" name="1">
            <el-form-item
              v-for="item1 in manyParamsArr"
              :key="item1.attr_id"
              :label="item1.attr_name"
            >
              <template>
                <el-checkbox-group v-model="checkList">
                  <el-checkbox
                    v-for="(item2, index) in item1.attr_vals"
                    :key="index"
                    :label="item2"
                  >
                  </el-checkbox>
                </el-checkbox-group>
              </template>
            </el-form-item>
          </el-tab-pane>
          <!-- 静态属性 -->
          <el-tab-pane label="商品属性" name="2">
            <el-form-item
              v-for="item in onlyParamsArr"
              :key="item.attr_id"
              :label="item.attr_name"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <!-- action:指定图片上传api接口 基准地址
           :on-preview :当点击图片时会触发该事件进行预览操作,处理图片预览
            :on-remove : 当用户点击图片右上角的X号是触发执行 
           :on-success :当用户点击图片上传图片并成功上传时触发
           list-type :设置预览图片方式
           :headers : 设置上传图片的请求头 -->
            <el-upload
              list-type="picture-card"
              action="http://www.ysqorz.top:8888/api/private/v1/upload"
              :on-preview="previewImg"
              :headers="headerObj"
              :on-success="successImage"
              :on-remove="handleRemove"
              multiple
            >
              <i class="el-icon-plus"></i>
            </el-upload>
            <!-- <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="" />
            </el-dialog> -->
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <!-- 富文本 -->
            <quill-editor v-model="addGoodsData.goods_introduce">
            </quill-editor>

            <el-button type="primary" class="mt30" @click="addGoodsOk"
              >添加商品</el-button
            >
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "goodsadd",
  data() {
    return {
      //请求头
      headerObj: {
        Authorization: sessionStorage.getItem("token"),
      },
      //checkBox复选框
      checkList: [],
      // 商品分类数据源数组
      options: [],
      defaultProps: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //级联选择器选中的数值
      checkArr: [],
      active: 0,
      activeName: "0",
      addGoodsData: {
        goods_name: "",
        goods_price: "320",
        goods_weight: "20",
        goods_number: "10",
        goods_cat: "",
        //商品介绍
        goods_introduce: "",
        //上传的图片数组 [{pic:'上传图片返回的临时路径'}]
        pics: [],
        //动态 /静态参数
        attrs: [],
      },
      //动态参数
      manyParamsArr: [],
      //静态参数
      onlyParamsArr: [],
    };
  },
  computed: {
    //最后一级id
    current_cate_id() {
      return this.checkArr[this.checkArr.length - 1];
    },
  },
  methods: {
    //点击确定将新的商品信息添加到后台
    async addGoodsOk() {
      //数据处理
      this.addGoodsData.goods_cat = this.checkArr.join(",");
      //先拿到动态参数数组
      const dtArr = this.manyParamsArr.map((item) => {
        return { attr_id: item.attr_id, attr_value: item.attr_vals.join(",") };
      });
      //在拿到静态参数数组
      const jtArr = this.onlyParamsArr.map((item) => {
        return { attr_id: item.attr_id, attr_value: item.attr_vals };
      });
      console.log(dtArr, "动态");
      console.log(jtArr, "静态");
      //最后合并动,静参数
      this.addGoodsData.attrs = [...dtArr, ...jtArr];
      const addFormData = JSON.parse(JSON.stringify(this.addGoodsData));

      //调用添加商品接口提交到后台
      await this.$http.addGoodsApi(addFormData);
      //跳转到商品列表，查看刚才添加的商品
      this.$router.push("/goods");
    },

    //tabs切换离开之前触发
    beforeLeave(newactive, oldactive) {
      // console.log(newactive, "新值");
      // console.log(oldactive, "旧值");
      if (oldactive === "0" && this.checkArr.length !== 3) {
        this.$message.error("请选择商品分类");
        return false;
      }
    },

    //调用分类接口Api
    async getGoodsCateList() {
      const res = await this.$http.getGoodsCataListApi();
      console.log(res, "调用分类接口Api");
      this.options = res;
    },
    //上传成功触发
    successImage(res) {
      console.log(res, "上传图片成功");
      let picObj = { pic: res.data.tmp_path };
      this.addGoodsData.pics.push(picObj);
      console.log(this.addGoodsData.pics, "图片数组");
    },
    //删除上传图片触发
    handleRemove(res) {
      console.log(res, "指定删除的上传图片");
      const index = this.addGoodsData.pics.findIndex((item) => {
        return item.pic === res.response.data.tmp_path;
      });
      console.log(index, "删除的下标");
      //然后在数组中删除元素
      this.addGoodsData.pics.splice(index, 1);
      console.log(this.addGoodsData.pics);
    },
    //触发该事件进行预览图片
    previewImg() {},

    //点击tab栏时触发
    async handleClick() {
      // console.log(this.activeName);
      let result;
      if (this.activeName === "1") {
        // 分类动态参数
        result = await this.$http.getGoodsParamsListApi(this.current_cate_id);

        result.forEach((item) => {
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(",") : [];
        });
        console.log(result, "分类动态参数");
        this.manyParamsArr = result;
      } else if (this.activeName === "2") {
        // 静态参数
        result = await this.$http.getGoodsParamsListApi(
          this.current_cate_id,
          "only"
        );
        console.log(result, "分类静态参数");
        this.onlyParamsArr = result;
      }
    },
    //监听级联选择器事件
    handleChange() {
      console.log("监听级联事件");
    },
  },
  mounted() {},
  created() {
    this.getGoodsCateList();
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-step__title {
  font-size: 16px;
}
::v-deep(.ql-editor) {
  height: 300px;
}
</style>
